package cn.keepme.common.utils;

import java.security.MessageDigest;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * @author sjz
 */
public class Coder {
	public static final String KEY_SHA = "SHA";  
    public static final String KEY_MD5 = "MD5";  
  
    /** 
     * MAC算法可选以下多种算法 
     *  
     * <pre> 
     * HmacMD5  
     * HmacSHA1  
     * HmacSHA256  
     * HmacSHA384  
     * HmacSHA512 
     * </pre> 
     */  
    public static final String KEY_MAC = "HmacMD5";  
  
    /** 
     * BASE64解密 
     *  
     * @param key 
     * @return 
     * @throws Exception 
     */  
    public static byte[] decryptBASE64(String key) throws Exception {  
    	return new Base64().decode(key);
    }  
  
    /** 
     * BASE64加密 
     *  
     * @param key 
     * @return 
     * @throws Exception 
     */  
    public static String encryptBASE64(byte[] key) throws Exception {  
    	return new Base64().encodeAsString(key);
    }  
  
    /** 
     * MD5加密 
     *  
     * @param data 
     * @return 
     * @throws Exception 
     */  
    public static byte[] encryptMD5(byte[] data) throws Exception {  
  
        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
        md5.update(data);  
  
        return md5.digest();  
  
    }  
  
    /** 
     * SHA加密 
     *  
     * @param data 
     * @return 
     * @throws Exception 
     */  
    public static byte[] encryptSHA(byte[] data) throws Exception {  
  
        MessageDigest sha = MessageDigest.getInstance(KEY_SHA);  
        sha.update(data);  
  
        return sha.digest();  
  
    }  
  
    /** 
     * 初始化HMAC密钥 
     *  
     * @return 
     * @throws Exception 
     */  
    public static String initMacKey() throws Exception {  
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
  
        SecretKey secretKey = keyGenerator.generateKey();  
        return encryptBASE64(secretKey.getEncoded());  
    }  
  
    /** 
     * HMAC加密 
     *  
     * @param data 
     * @param key 
     * @return 
     * @throws Exception 
     */  
    public static byte[] encryptHMAC(byte[] data, String key) throws Exception {  
  
        SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);  
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());  
        mac.init(secretKey);  
  
        return mac.doFinal(data);  
  
    }
    
    
    
    
    public static String decodeBASE64(String str) throws Exception{
    	byte[] data=Coder.decryptBASE64(str);
    	return new String(data);
    }
    
    public static String encodeBASE64(String str) throws Exception{
    	byte[] key=str.getBytes();
    	return Coder.encryptBASE64(key);
    }
    
    public static String encodeMD5(String str) throws Exception{
    	if(str==null){str="";}
    	byte[] data=Coder.encryptMD5(str.getBytes());
    	return Coder.bytesToString(data);
    }
    
    public static String encodeSHA(String str) throws Exception{
    	if(str==null){str="";}
    	byte[] data=Coder.encryptSHA(str.getBytes());
    	return Coder.bytesToString(data);
    }
    
    public static String bytesToString(byte[] data){
    	StringBuffer md5StrBuff = new StringBuffer();
		for (int i=0;i<data.length;i++){
			if (Integer.toHexString(0xFF & data[i]).length()==1) {
				md5StrBuff.append("0").append(
				Integer.toHexString(0xFF & data[i]));
			}else{
				md5StrBuff.append(Integer.toHexString(0xFF & data[i]));
			}
		}
		return md5StrBuff.toString();
    }
    
    
    public static void main(String[] args) throws Exception{
    	//String str="698d51a19d8a121ce581499d7b701668";//111
    	//String str="96e79218965eb72c92a549dd5a330112";//111111
    	//String str="e10adc3949ba59abbe56e057f20f883e";//123456
    	//String str="c33367701511b4f6020ec61ded352059";//654321
    	//String str="202cb962ac59075b964b07152d234b70";//123
    	
    	//String base64=Coder.encodeBASE64(str);
    	//System.out.println(base64);
    	//System.out.println(Coder.decodeBASE64(base64));
    	//System.out.println(Coder.encodeMD5(str));
    	//System.out.println(Coder.encodeSHA(str));
    	//System.out.println(MD5.doMD5(str));
    	
//    	String str="你好as23!@  #$%^&*()";
//    	渠道id +计费金额+客户端ip +扩展字段+游戏名称（urlencode）+计费点名称（urlencode）+key
    	String str="!&=uyo&!'&2u3*P^V&6?&!&7QF,Q0[q&000000";
//    				11034124.202.176.250555%E7%8C%AA%E7%8C%AA%E6%88%91%E8%B8%A26yc6t499
    	String s1=Coder.encodeMD5(str);
    	
    	System.out.println(s1);
    	
//    	String s2=Coder.encodeSHA(str);
//    	System.out.println(s2);

    }
}
